package com.translate.web.api.controller;

import java.math.BigDecimal;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.NativeWebRequest;

import com.translate.vo.WithdrawalVO;
import com.response.vo.ResponseModel;
import com.controller.base.AbstractCommonController;
import com.translate.web.param.WithdrawalUpdateParam;
import com.translate.web.param.UserIdParam;
import com.translate.web.param.WithdrawalAddParam;
import com.translate.entity.CommissionManagement;
import com.translate.entity.NoticeCenter;
import com.translate.entity.User;
import com.translate.entity.Withdrawal;
import com.translate.web.service.ICommissionManagementService;
import com.translate.web.service.INoticeCenterService;
import com.translate.web.service.IUserService;
import com.translate.web.service.IWithdrawalService;
import com.basic.service.contract.IBaseService;

@RestController
@RequestMapping("withdrawal")
public class WithdrawalController extends AbstractCommonController<Withdrawal, UserIdParam> {
	private IWithdrawalService service;

	private IUserService userService;
	
	ICommissionManagementService commissionManagementService;
	
	INoticeCenterService centerService;
	
	@Resource
	public void setCommissionManagementService(ICommissionManagementService commissionManagementService) {
		this.commissionManagementService = commissionManagementService;
	}

	@Resource
	public void setCenterService(INoticeCenterService centerService) {
		this.centerService = centerService;
	}

	public WithdrawalController(IWithdrawalService service) {
		this.service = service;
	}

	/**
	 * 添加(提现)
	 * 
	 * @param <V>
	 * @param param
	 * @return
	 */
	@PostMapping("/add")
	@Transactional
	public ResponseModel<Integer> insert(@RequestBody @Validated WithdrawalAddParam param,
			NativeWebRequest httpServletRequest) {
		long userId = this.getUserId(httpServletRequest);

		CommissionManagement commissonManagentForUpdate = commissionManagementService.getCommissonManagentForUpdate(userId);

		BigDecimal withdrawableAmount = commissonManagentForUpdate.getWithdrawableAmount();

		BigDecimal amount = param.getAmount();
		
		// 提现金额 小于待提现金额
		if (withdrawableAmount.compareTo(amount) == -1 ) {
			return ResponseModel.errorMsg(0, "提现金额不能小于待提现金额");
		}
		
		param.setUserId(userId);
		
		User userInfoToCache = userService.getUserInfoToCache((int) userId);

		String realname = userInfoToCache.getRealname();
		param.setUserName(realname);

		int id = this.service.parseInsert(param, Withdrawal.class);
		
		//更新 佣金余额
		// 提 10元 冻结 10元
		CommissionManagement update =  new CommissionManagement();
		
		update.setFreezeAmount(amount.add(commissonManagentForUpdate.getFreezeAmount()));
		
		update.setWithdrawableAmount(withdrawableAmount.subtract(amount));
		
		update.setId(commissonManagentForUpdate.getId());
		
		commissionManagementService.updateByPrimaryKeySelective(update);

		// 您发起的【YxXX】提现申请已提交，平台将在1-3个工作日内完成审核，请耐心等待。

		NoticeCenter center = new NoticeCenter();

		center.setUserId((int) userId);

		center.setJump((byte) 0);
		
		center.setNoticeContent("您发起的【￥" + amount + "】提现申请已提交，平台将在1-3个工作日内完成审核，请耐心等待。");

		center.setPlatform((byte) 0);

		center.setNoticeType((byte) 5);

		center.setTitle("提现申请");

		center.setTypeId(id);

		centerService.insert(center);
		
		// 向管理端发送 消息 张三(ID:12345678)申请提现【¥xXX】，请及时查看处理!
		
		center = new NoticeCenter();

		center.setUserId((int) userId);

		center.setNoticeContent(realname + "(ID:" + userId + ") 申请提现【￥" + amount + "】请及时查看处理!");

		center.setPlatform((byte) 1);

		center.setNoticeType((byte) 5);

		center.setTitle("提现审核通知");

		center.setTypeId(id);

		centerService.insert(center);

		return ResponseModel.<Integer>success(id);
	}

	@Override
	protected IBaseService<Withdrawal> getBaseService() {
		return this.service;
	}

	@Override
	protected void frontProcesserVO(UserIdParam param, NativeWebRequest object) {
		// TODO Auto-generated method stub
		param.setUserId((int) this.getUserId(object));
		super.frontProcesserVO(param, object);
	}

	/**
	 * 添加
	 * 
	 * @param <V>
	 * @param param
	 * @return
	 */
	@PostMapping("/update")
	public ResponseModel<Integer> update(@Validated @RequestBody WithdrawalUpdateParam param,
			HttpServletRequest httpServletRequest) {
		int res = this.service.parseUpdate(param, Withdrawal.class);
		return ResponseModel.<Integer>success(res);
	}

	protected <VO> Class<VO> getTarget() {
		return (Class<VO>) WithdrawalVO.class;
	}

	@Resource
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
}